﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Web.Configuration;
using System.ServiceModel.Configuration;
using BizTalkOneService;

namespace BizTalkOneClient
{
    public partial class Default : System.Web.UI.Page
    {
        private string ColorSucceed = "#009900";
        private string ColorError = "#CC0000";
        private string ColorActive = "#CCBB00";
        private string ColorWarning = "#CC6600";
        private string ColorGeneral = "#3366AA";
        private bool LabelColor = true;

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.AppendHeader("Refresh", "10");
                BizTalkCounters Biztalkcounters = new BizTalkCounters();
                int i = 1;
                foreach(var groupname in Biztalkcounters.GetGroupNames())
                {
                    Panel GroupPanel = new Panel();
                    GroupPanel.ID = groupname;
                    GroupPanel.CssClass = "BizTalkGroup";
                    form1.Controls.Add(GroupPanel);
                    Label BizTalkGroupLabel = new Label();
                    BizTalkGroupLabel.Text = groupname;
                    BizTalkGroupLabel.CssClass = "BizTalkGroupLabel";
                    BizTalkGroupLabel.ID = "BizTalkGroupLabel" + i++;
                    GroupPanel.Controls.Add(BizTalkGroupLabel);
                    
                    foreach (var categoryname in Biztalkcounters.GetCategoryList(groupname))
                    {
                            Panel CategoryPanel = new Panel();
                            CategoryPanel.ID = categoryname;
                            CategoryPanel.CssClass = "CategoryPanel";
                            GroupPanel.Controls.Add(CategoryPanel);
                            Label CategoryLabel = new Label();
                            CategoryLabel.Text = categoryname;
                            CategoryLabel.CssClass = String.Format("CategoryLabel",categoryname);
                            CategoryLabel.ID = String.Format("{0}Label", categoryname);
                            CategoryPanel.Controls.Add(CategoryLabel);
                            
                            foreach (var countername in Biztalkcounters.GetCounterByCategory(categoryname, groupname))
                            {
         

                                switch (countername.CounterCategory) 
                                { 
                                    case "Error":
                                        SetErrorCounters(countername, CategoryPanel);
                                    break;

                                    case "Throttle":
                                        SetThrottleCounters(countername, CategoryPanel);
                                    break;

                                    case "Instance":
                                        if (countername.CounterValue != 0)
                                        {
                                            switch (countername.CounterName)
                                            {
                                                case "Active":
                                                    SetCounters(countername, CategoryPanel, ColorActive);
                                                    break;
                                                case "Dehydrated":
                                                    SetCounters(countername, CategoryPanel, ColorActive);
                                                    break;
                                                case "ReadyToRun":
                                                    SetCounters(countername, CategoryPanel, ColorActive);
                                                    break;
                                                case "Suspended":
                                                    SetCounters(countername, CategoryPanel, ColorError);
                                                    break;
                                                case "Suspended-NR":
                                                    SetCounters(countername, CategoryPanel, ColorError);
                                                    break;

                                            }
                                        } else 
                                        SetCounters(countername, CategoryPanel, "");
                                    break;

                                    case "Host":
                                    if (countername.CounterValue != 0)
                                    {
                                        switch (countername.CounterName.Substring(0, Math.Min(7, countername.CounterName.Length)))
                                        {
                                            case "Running":
                                                SetCounters(countername, CategoryPanel, ColorSucceed);
                                                break;
                                            case "Start-P":
                                                SetCounters(countername, CategoryPanel, ColorActive);
                                                break;
                                            case "Stop-Pe":
                                                SetCounters(countername, CategoryPanel, ColorActive);
                                                break;
                                            case "Stopped":
                                                SetCounters(countername, CategoryPanel, ColorError);
                                                break;
                                        }
                                    } else
                                    SetCounters(countername, CategoryPanel, "");
                                    break;

                                    case "SendPort":
                                    if (countername.CounterValue != 0)
                                    {
                                        switch (countername.CounterName.Substring(0, Math.Min(7, countername.CounterName.Length)))
                                        {
                                            case "Started":
                                                SetCounters(countername, CategoryPanel, ColorSucceed);
                                                break;
                                            case "Stopped":
                                                SetCounters(countername, CategoryPanel, ColorError);
                                                break;
                                            case "Unbound":
                                                SetCounters(countername, CategoryPanel, ColorError);
                                                break;
                                            case "Total":
                                                SetCounters(countername, CategoryPanel, ColorGeneral);
                                                break;
                                        }
                                    } else
                                        SetCounters(countername, CategoryPanel, "");
                                    break;
                                    
                                    case "ReceiveLocation":
                                    if (countername.CounterValue != 0)
                                    {
                                        switch (countername.CounterName.Substring(0, Math.Min(7, countername.CounterName.Length)))
                                        {
                                            case "Enabled":
                                                SetCounters(countername, CategoryPanel, ColorSucceed);
                                                break;
                                            case "Disable":
                                                SetCounters(countername, CategoryPanel, ColorError);
                                                break;
                                            case "Total":
                                                SetCounters(countername, CategoryPanel, ColorGeneral);
                                                break;
                                        }
                                    }
                                    else
                                        SetCounters(countername, CategoryPanel, "");
                                    break;

                                    case "SQLJob":
                                    if (countername.CounterValue != 0)
                                    {
                                        switch (countername.CounterName.Substring(0, Math.Min(7, countername.CounterName.Length)))
                                        {
                                            case "Cancell":
                                                SetCounters(countername, CategoryPanel, ColorGeneral);
                                                break;
                                            case "Failed ":
                                                SetCounters(countername, CategoryPanel, ColorError);
                                                break;
                                            case "InProgr":
                                                SetCounters(countername, CategoryPanel, ColorActive);
                                                break;
                                            case "Retry (":
                                                SetCounters(countername, CategoryPanel, ColorActive);
                                                break;
                                            case "Succeed":
                                                SetCounters(countername, CategoryPanel, ColorSucceed);
                                                break;
                                            case "Unknown":
                                                SetCounters(countername, CategoryPanel, ColorGeneral);
                                                break;
                                            case "Disable":
                                                SetCounters(countername, CategoryPanel, ColorGeneral);
                                                break;
                                            case "Total":
                                                SetCounters(countername, CategoryPanel, ColorGeneral);
                                                break;
                                        }
                                    }
                                    else
                                        SetCounters(countername, CategoryPanel, "");
                                    break;

                                    case "Orchestration":
                                    if (countername.CounterValue != 0)
                                    {
                                        switch (countername.CounterName.Substring(0, Math.Min(7, countername.CounterName.Length)))
                                        {
                                            case "Started":
                                                SetCounters(countername, CategoryPanel, ColorSucceed);
                                                break;
                                            case "Stopped":
                                                SetCounters(countername, CategoryPanel, ColorError);
                                                break;
                                            case "Unenlis":
                                                SetCounters(countername, CategoryPanel, ColorError);
                                                break;
                                            case "Total":
                                                SetCounters(countername, CategoryPanel, ColorGeneral);
                                                break;
                                        }
                                    }
                                    else
                                        SetCounters(countername, CategoryPanel, "");
                                    break;

                                    case "File":
                                    if (countername.CounterValue != 0)
                                    {
                                        SetCounters(countername, CategoryPanel, ColorGeneral);
                                        break;
                                    }
                                    else
                                        SetCounters(countername, CategoryPanel, "");
                                    break;

                                    case "EsbException":
                                    if (countername.CounterName == "Error")
                                        break;
                                    if (countername.CounterValue != 0)
                                    {
                                        SetCounters(countername, CategoryPanel, ColorActive);
                                        break;
                                    }
                                    else
                                        SetCounters(countername, CategoryPanel, "");
                                    break;

                                    case "FreeDiskSpace":
                                    if (countername.CounterValueString != null)
                                    {
                                        SetDiskCounters(countername, CategoryPanel, ColorGeneral);
                                        break;
                                    }
                                    else
                                        SetDiskCounters(countername, CategoryPanel, "" );
                                    break;
                                    


                                    default:
                                    SetCounters(countername, CategoryPanel, "");
                                        break;
                                }

                                
                            }
                    }  
                }
            }

            protected void SetErrorCounters(Counter counter, Panel panel) 
            {
                Panel CounterPanel = new Panel();
                CounterPanel.ID = String.Format("{0}CounterPanelId", counter.CounterCategory);
                CounterPanel.CssClass = "ErrorCounterPanel";
                panel.Controls.Add(CounterPanel);
                Label Name = new Label();
                Name.CssClass = "ErrorCounterName";
                Name.ID = String.Format("{0}CounterNameId", counter.CounterCategory);
                Name.Text = String.Format("{0}: {1}", counter.CounterName, counter.CounterValueString);
                Name.ForeColor = Color.FromName(ColorError);
                CounterPanel.Controls.Add(Name);
            }

            protected void SetCounters(Counter counter, Panel panel, string color)
            {
                Panel CounterPanel = new Panel();
                CounterPanel.ID = String.Format("{0}Panel ",counter.CounterName);
                CounterPanel.CssClass = "CounterPanel";
                CounterPanel.Width = 75;
                CounterPanel.Height = 75;
                
                panel.Controls.Add(CounterPanel);

                Label Name = new Label();
                Name.CssClass = "CounterName";
                Name.ID = String.Format("{0}Value",counter.CounterCategory) ;
                //Name.Style.Add("text-align", "center");
                Name.Width = 75;
                Name.Height = 30;
                if (LabelColor) { Name.Style.Add("background-color", color); }
                Name.Text = counter.CounterName;
                CounterPanel.Controls.Add(Name);

                Label Value = new Label();
                Value.CssClass = "CounterValue";
                Value.ID = String.Format("{0}Value", counter.CounterCategory);
                Value.Width = 75;
                Value.Height = 45;
                Value.Text = counter.CounterValue.ToString();
                if (LabelColor && counter.CounterValue != 0) { 
                    Value.Style.Add("background-color", color);
                    Value.Style.Add("color", "#FFFFFF");
                }
                Value.Style.Add("text-align", "center");
                Value.Style.Add("vertical-align","middle");
                if(Value.Text.Length < 5)
                 Value.Style.Add("font-size", "30px");
                if (Value.Text.Length >= 5 && Value.Text.Length < 6)
                { 
                    Value.Style.Add("font-size", "23px");
                    
                }
                if (Value.Text.Length >= 6)
                    Value.Style.Add("font-size", "19px");
                Value.ForeColor = Color.FromName(color);
                CounterPanel.Controls.Add(Value);

                    //Label ServerName = new Label();
                    //Value.CssClass = "ServerName";
                    //Value.ID = String.Format("{0}ServerName", counter.ServerName);
                    //Value.Width = 75;
                    //Value.Height =10;
                    //ServerName.Style.Add("background-color", color);
                    //ServerName.Style.Add("font-size", "2px");
                    //if (counter.ServerName != null) { Value.Text = counter.ServerName.ToString(); }
                    //CounterPanel.Controls.Add(ServerName);
                
            }

            protected void SetDiskCounters(Counter counter, Panel panel, string color )
            {
                color = "";
                if (counter.CounterValue <= 10)
                    color = ColorError;
                else if (counter.CounterValue >= 10 && counter.CounterValue < 20)
                    color = ColorWarning;
                else if (counter.CounterValue >= 20 && counter.CounterValue < 30)
                    color = ColorWarning;
                else if (counter.CounterValue >= 30)
                    color = ColorSucceed;
                Panel CounterPanel = new Panel();
                CounterPanel.ID = counter.CounterName;
                CounterPanel.CssClass = "CounterPanel";
                CounterPanel.Style.Add("width", "75px");
                CounterPanel.Style.Add("height", "75px");
                panel.Controls.Add(CounterPanel);

                Label Name = new Label();
                Name.CssClass = "CounterName";
                Name.ID = counter.CounterCategory + "_" + counter.CounterName;
                Name.Width = 75;
                Name.Height = 30;
                Name.Style.Add("text-align", "center");
                if (LabelColor) { Name.Style.Add("background-color", color); }
                Name.Text = counter.CounterName;
                //Name.Text = counter.CounterValueString;
                CounterPanel.Controls.Add(Name);

                Label Value = new Label();
                Value.CssClass = "CounterValue";
                Value.ID = String.Format("{0}", counter.CounterCategory);
                Value.Width = 75;
                Value.Height = 45;
                Value.Text = counter.CounterValueString.ToString();
                Value.Style.Add("text-align", "center");
                Value.Style.Add("font-size", "13px");
                Value.Style.Add("font-weight", "bold");
                //Value.Style.Add("background-color", "#3366FF");
                
                Value.ForeColor = Color.FromName(color);
                //Value.ForeColor = Color.FromName("#6666FF");
                CounterPanel.Controls.Add(Value);
            }

            protected void SetThrottleCounters(Counter counter, Panel panel) 
            {

                Panel CounterPanel = new Panel();
                CounterPanel.ID = counter.CounterName;
                CounterPanel.CssClass = "CounterPanel";
                CounterPanel.Style.Add("width", "600px");
                //CounterPanel.Style.Add("height", "60px");
                panel.Controls.Add(CounterPanel);

                Label ServerName = new Label();
                ServerName.CssClass = "ThrottleServerName";
                //ServerName.ID = counter.CounterCategory + "_" + counter.ServerName;
                ServerName.Width = 100;
                //ServerName.Height = 20;
                ServerName.Text = counter.ServerName;
                CounterPanel.Controls.Add(ServerName);

                Label ThrottleName = new Label();
                ThrottleName.CssClass = "ThrottleCounterName";
                //ThrottleName.ID = counter.CounterCategory + "_" + counter.CounterName;
                ThrottleName.Width = 220;
                //ThrottleName.Height = 20;
                ThrottleName.Style.Add("text-align", "left");
                ThrottleName.Text = counter.CounterName;
                CounterPanel.Controls.Add(ThrottleName);

                Label ThrottleValue = new Label();
                ThrottleValue.CssClass = "ThrottleCounterValue";
                //ThrottleValue.ID = counter.CounterCategory + "_" + counter.CounterName;
                ThrottleValue.Width = 260;
                //ThrottleValue.Height = 20;
                switch (counter.CounterValue.ToString())
                {
                    case "1":
                        ThrottleValue.Text = "Imbalanced message delivery rate";
                        ThrottleValue.ForeColor = Color.FromName("#ffff00");
                        break;
                    case "2":
                        ThrottleValue.Text = "Imbalanced message publishing rate ";
                        ThrottleValue.ForeColor = Color.FromName("#ffff00");
                        break;
                    case "3":
                        ThrottleValue.Text = "High in-process message count";
                        ThrottleValue.ForeColor = Color.FromName("#ee0000");
                        break;
                    case "4":
                        ThrottleValue.Text = "Process memory pressure";
                        ThrottleValue.ForeColor = Color.FromName("#ee0000");
                        break;
                    case "5":
                        ThrottleValue.Text = "System memory pressure";
                        ThrottleValue.ForeColor = Color.FromName("#ee0000");
                        break;
                    case "6":
                        ThrottleValue.Text = "Database growth";
                        ThrottleValue.ForeColor = Color.FromName("#ee0000");
                        break;
                    case "7":
                        ThrottleValue.Text = "";
                        break;
                    case "8":
                        ThrottleValue.Text = "High session count";
                        ThrottleValue.ForeColor = Color.FromName("#ee0000");
                        break;
                    case "9":
                        ThrottleValue.Text = "High thread count";
                        ThrottleValue.ForeColor = Color.FromName("#ee0000");
                        break;
                    case "10":
                        ThrottleValue.Text = "User override on delivery";
                        ThrottleValue.ForeColor = Color.FromName("#00ee00");
                        break;
                    case "11":
                        ThrottleValue.Text = "User override on publishing";
                        ThrottleValue.ForeColor = Color.FromName("#00ee00");
                        break;
                    default:
                        ThrottleValue.Text = "";
                        break;

                }
                CounterPanel.Controls.Add(ThrottleValue);
            }
            
    
    }
}